import os

from PIL import Image, ImageDraw, ImageFont

import math
import random

class Avatar(object):
	
	def __init__(self):
		W = 512
		H = W
		pkg_dir = os.path.dirname(__file__)#os.curdir
		logoList = [n for n in os.listdir(pkg_dir) if n.endswith('.png') or n.endswith('.jpg')]
		self.img = Image.open(random.choice(logoList))
		self.img = self.img.resize((W,H),Image.ANTIALIAS)
		self.draw = ImageDraw.Draw(self.img)
		self.ColorPalette = []
		self.ColorPalette.append((138,194,240,255))
		self.ColorPalette.append((255,126,131,255))
		self.ColorPalette.append((139,222,228,255))
		self.ColorPalette.append((255,206,132,255))
		self.ColorPalette.append((142,216,149,255))
		self.ColorPalette.append((255,124,180,255))
		self.ColorPalette.append((162,131,195,255))
		self.ColorPalette.append((222,131,195,255))
		self.ColorPalette.append((255,166,47,255))
		self.centerPos = []
		self.centerPos.append((0.2,0.2))
		self.centerPos.append((0.8,0.2))
		self.centerPos.append((0.8,0.8))
		self.centerPos.append((0.2,0.8))
		self.centerPos.append((0.3854,0.3854))
		self.centerPos.append((0.6146,0.3854))
		self.centerPos.append((0.6146,0.6146))
		self.centerPos.append((0.3854,0.6146))
		self.centerPos.append((0.5,0.5))

		# pkg_dir = os.path.dirname(__file__)
		# img_dir = os.path.join(pkg_dir, 'logo.jpg')
		# self.logoImg = Image.open(img_dir).convert(self.img.mode)
		# self.logoImg = self.logoImg.resize(self.img.size, Image.ANTIALIAS)
	
	@property
	def size(self):
		return self.img.size

	@property
	def W(self):
		return self.size[0]
	
	@property
	def H(self):
		return self.size[1]
	
	@property
	def center(self):
		return (1.0 / 2, 1.0 / 2)
	
	def get_path(self):
	    return 'avatar/' + self.img_path
	
	
	def paint_avatar(self,plain):
		outR = map(lambda a:(a[0]*self.W, a[1]*self.H),self.centerPos[0:4])
		inR = map(lambda a:(a[0]*self.W, a[1]*self.H),self.centerPos[4:8])
		# self.draw.polygon(outR,fill=(43, 27, 23, 100), outline=None);
		cpColor = self.ColorPalette[:];
		cpColor.append((65, 163, 23, 255))
		cpColor.append((43, 27, 23,255))
		self.draw.polygon(inR,fill=random.choice(cpColor), outline=None);
		for i in range(4):
			self.draw.line([outR[i],outR[(i+1)%4]],fill='#000000', width=4)
			self.draw.line([inR[i],inR[(i+1)%4]],fill='#000000', width=4)
			self.draw.line([outR[i],inR[i]],fill='#000000', width=2)
			# self.draw.line([inR[i],(self.W * 0.5, self.H * 0.5)],fill='#000000', width=2)
		fontSize = 35
		radius = 0.05
		pkg_dir = os.path.dirname(__file__)
		font_path = os.path.join(pkg_dir,'fonts', 'OpenSans-Bold.ttf')
		ttFont = ImageFont.truetype(font_path,fontSize)
		for i in range(9):
			self.draw_circle(self.centerPos[i], radius=radius, fill_color=self.ColorPalette[i], outline_color='#000000')
			x = self.centerPos[i][0] * self.W - fontSize+ radius * self.W
			y = self.centerPos[i][1] * self.H - fontSize * 1.45+ radius * self.W
			self.draw.text((x,y), plain[i], fill=(0,0,0), font=ttFont)

		self.draw.text((self.W*0.265,self.H*0.855 - fontSize), 'SN:'+plain[0:9], fill=random.choice(cpColor), font=ttFont)

		# self.img  = Image.blend(self.img,self.logoImg,0.1)

		self.save(plain,self.size)
		self.save(plain,(128,128))
		self.save(plain,(256,256))
		self.save(plain,(128,128))
		self.save(plain,(64,64))

	def draw_rectangle(self, box, color):
		'''Draw a rectangle
		'''
		
		left = box[0] * self.W
		right = box[2] * self.W
		upper = box[1] * self.H
		lower = box[3] * self.H
		self.draw.rectangle([left, upper, right, lower], fill=None, outline=color)
		
	def draw_circle(self, center, radius, fill_color, outline_color):
		'''Draw a circle'''
		R = radius * self.W
		D = 720*4
		d = 360.0 / D
		fd = math.pi / 180.0
		pos = [];
		for i in range(D):
			x = center[0] * self.W + math.cos(d * i * fd)*R
			y = center[1] * self.W + math.sin(d * i * fd)*R
			pos.append((x,y))
		self.draw.polygon(pos,fill=fill_color, outline=None);
		for i in range(D):
			self.draw.line([pos[i],pos[(i+1)%D]],fill=outline_color, width=2)
		# left = int(round((center[0] - radius) * self.W))
		# right = int(round((center[0] + radius) * self.W))
		# upper = int(round(center[1] * self.H - radius * self.W))
		# lower = int(round(center[1] * self.H + radius * self.W))
		# self.draw.ellipse((left, upper, right, lower), fill=fill_color, outline=outline_color)


	def draw_arc(self, box, start, end, color):
		'''Draw an arc'''
		
		left = int(round(box[0] * self.W))
		upper = int(round(box[1] * self.H))
		right = int(round(box[2] * self.W))
		lower = int(round(box[3] * self.H))
		
		start_in_deg = int(round(start * 360))
		end_in_deg = int(round(end * 360))
		self.draw.arc((left, upper, right, lower), start_in_deg, end_in_deg, fill=color)
	
	def save(self, img_name,size, img_dir=None):
		if img_dir is None:
			pkg_dir = os.path.dirname(__file__)
			img_dir = os.path.join(pkg_dir, '..', 'static', 'avatar')
		full_img_path = os.path.join(img_dir, '{}_{}.png'.format(img_name,size[0]))
		self.img_path = '{}_{}.png'.format(img_name,size[0])
		# self.imgSized = self.img.resize(size, Image.ANTIALIAS)
		# self.imgSized.save(full_img_path, quality=100, optimize=True)
		self.img.thumbnail(size, Image.ANTIALIAS)
		self.img.save(full_img_path,'png')

#test function
if __name__ == '__main__':
	sp = Avatar()
	sp.paint_avatar("Zhenghongkai")
	sp.img.show()
